/**
 * 会员用户管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索">
          <el-select size="small" v-model="formInline.category" placeholder="请选择会员类型" clearable>
            <el-option v-for="type in category" :label="type.key" :value="type.value" :key="type.value" ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.nickName" placeholder="输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.id" placeholder="输入编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="id" label="用户编号" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="nickName" label="昵称" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="gender" label="性别" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="address" label="地址" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="balance" label="余额" min-width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="category" label="会员类型" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="overTimes" label="剩余次数" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="deadline" label="到期时间" min-width="120">
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">更改会员卡</el-button>
          <el-button size="mini" type="danger" @click="reset(scope.$index, scope.row)">重置</el-button>
          <el-button size="mini" type="success" @click="rechargeEdit(scope.$index, scope.row)">充值</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 修改会员类型  -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="会员卡" prop="category">
          <el-select size="small" v-model="editForm.category" placeholder="请选择" class="userRole">
            <el-option label="普通会员" value="普通会员"></el-option>
            <el-option label="年卡会员" value="年卡会员"></el-option>
            <el-option label="月卡会员" value="月卡会员"></el-option>
            <el-option label="次卡会员" value="次卡会员"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- 充值 -->
    <el-dialog title="数据权限" :visible.sync="rechargeshow" width="30%" @click='closeDialog("perm")'>
      <el-form label-width="120px" :model="rechargeForm">
        <el-form-item label="金额:" prop="number">
          <el-input size="small" v-model="rechargeForm.number" auto-complete="off" placeholder="请输入数额" type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("recharge")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="recharge('rechargeForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  UserList,
  UserUpdateCategory,
  userDelete,
  userReset,
  UserRecharge
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: '添加用户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      rechargeshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      rechargeForm:{
          id:'',
          number:''
      },
      category: [
        { key: '普通会员', value: '普通会员' },
        { key: '次卡会员', value: '次卡会员' },
        { key: '月卡会员', value: '月卡会员' },
        { key: '年卡会员', value: '年卡会员' }
      ],
      // 编辑与添加
      editForm: {
        id: '',
        category: '',
        token: localStorage.getItem('logintoken')
      },
      // 选择数据
      selectdata: [],
      // rules表单验证
      rules: {
        category: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        balance: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      },
      // 删除用户
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 重置会员
      resetpsd: {
        id: '',
        token: localStorage.getItem('logintoken')
      },
      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
        id: '',
        nickName: '',
        category: ''
      },
      //用户数据
      userData: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
      //获取用户列表
      UserList(parameter).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.userData = res.data
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.total
        }
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      this.getdata(this.formInline)
      console.log(this.formInline);
      
    },
    handleEdit: function(index, row) {
        this.editFormVisible = true
        this.title = '修改会员卡'
        this.editForm.id = row.id
        this.editForm.category = row.category
    },
    //修改会员类型
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          UserUpdateCategory(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '数据保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'recharge') {
        this.rechargeshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },
    // 重置
    reset(index, row) {
      this.resetpsd.id = row.id
      this.$confirm('确定要重置会员账户财产吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          userReset(this.resetpsd)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '重置会员账户成功！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('重置密码失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消重置密码！'
          })
        })
    },
    rechargeEdit(index,row){
        this.rechargeshow = true
        this.rechargeForm.id=row.id
    },
    // 充值
    recharge(rechargeForm) {
        UserRecharge(this.rechargeForm)
        .then(res => {
            this.rechargeshow = false
            this.loading = false
            if (res.success) {
            this.getdata(this.formInline)
            this.rechargeForm.number=''
            this.$message({
                type: 'success',
                message: '数据保存成功！'
            })
            } else {
            this.$message({
                type: 'info',
                message: res.msg
            })
            }
        })
        .catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('保存失败，请稍后再试！')
        })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 